var api = require('API').order.order_list;
var SetFilter = require('set-filter');
var DateUtil = require('DateUtil');
var cols = [
    {
        field: 'create_time', headerName: '订单日期',
        cellRenderer: function(p){
            return DateUtil.format(p.value);
        },
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'name', headerName: '客户',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'shop_name', headerName: '门店',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'owner_id', headerName: '销售',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'insurance_id', headerName: '状态',
        cellRenderer:function(p){
            // console.log(p);
            return (p.value ? '已':'未') + '填写'
        },
        filterParams: {newRowsAction: 'keep'}
    },
    {
        headerName  : '操作',
        cellRenderer: function (p) {
            var container = document.createElement('span');

            var open = document.createElement('a');
            open.innerHTML = '<i class="fa fa-folder-open" style="padding:0 .25em" title="打开"></i>';
            open.addEventListener('click', function(){
                callbacks.open(p.data)
            });
            container.appendChild(open);


            return container;
        },
        suppressMenu:true,
        suppressSorting:true
    }
];

var getData = function (data) {

    return {
        getRows : function (p) {
            var qo = {};

            //pagination
            qo.offset = p.startRow;
            qo.limit = p.endRow - p.startRow;

            //sort
            if (p.sortModel.length > 0) {
                qo.sort = '';
                p.sortModel.forEach(function (val) {
                    qo.sort += (val.sort == 'desc' ? '-' : '+') + val.colId + ", ";
                });
            }
            //filter
            var search = p.filterModel;

            if (search) {
                Object.keys(search).forEach(function (val) {
                    var searchObj = search[val];
                    switch (searchObj.type){
                        case 'set':
                            qo[val] = "in," + searchObj.values.join(',');
                            break;
                        case 1:
                            qo[val] = "lk,%" + searchObj.filter + "%";
                            break;
                        case 2:
                            qo[val] = searchObj.filter;
                            break;
                    }
                });
            }
            if (data.phone){
                qo.phone = data.phone;
            }
            if (data.shop_id) {
                qo.shop_id = data.shop_id;
            }
            if (data.create_time) {
                qo.create_time = data.create_time;
            }
            api.search_insurance(qo, function (json, status, xhr) {
                var count = xhr.getResponseHeader('X-Total-Count');
                p.successCallback(json, Number(count));
            });
        },
        pageSize: 50

    }
};
var callbacks = {
    "open": function(){alert('open')}
};

var t = {
    grid:{
        //angularCompileRows     : true,
        columnDefs             : cols,
        rowData                : [],
        datasource             : null,
        rowModelType           : 'pagination',
        enableServerSideSorting: true,
        enableServerSideFilter : true,
        suppressCellSelection  : true,
        rowHeight              : 22,
        colWidth               : 100,
        enableColResize        : true,
        enableCellExpressions  : true,
        api                    : {},
        columnApi              : {},
        rowSelection           : 'single',
        localeText             : require('locale').aggrid
    },
    update: function (phone,shop_id,date_start,date_end) {
        var qo = {
            phone:phone,
            shop_id:shop_id,
            create_time:(date_start && date_end)?('btwn,' + date_start + ',' + date_end):(void 0)
        };

        // console.log(t);
        t.grid.api.setDatasource(getData(qo));
    },
    on: function(name, cb){
        callbacks[name] = cb;
    }
};

module.exports = t;
